package com.zxbg.service.impl.proportalServiceImpl;

import com.zxbg.dao.DepartmentDao;
import com.zxbg.dao.EmployeeDao;
import com.zxbg.domain.Department;
import com.zxbg.service.proportalService.PersonalCenterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Service
public class PersonalCenterServiceImpl implements PersonalCenterService {
    @Autowired
    EmployeeDao employeeDao;
    @Autowired
    DepartmentDao departmentDao;

    @Override
    public boolean updateAvatarImg(String newPicUrl, String employee_id, Integer tenant_id) {
        return employeeDao.updateAvatarImg(newPicUrl, employee_id, tenant_id) > 0;
    }

    @Override
    public boolean resetAvatar(String employee_id, Integer tenant_id) {
        return updateAvatarImg(null, employee_id, tenant_id);
    }

    @Override
    public List<String> getFullPathDept(String all_dept_id) {
        List<String> stringList = new ArrayList<>();
        if(all_dept_id != null && !all_dept_id.equals("") && !all_dept_id.equals("0")) {
            List<String> deptList = Arrays.asList(all_dept_id.split(","));
            for (String dept : deptList) {
                stringList.add(getParentId(Integer.parseInt(dept), 1));
            }
        }
        return stringList;
    }

    public String getParentId(Integer dept_id, Integer tag) {
        Department temp = departmentDao.getDeptById(dept_id);
        if(temp.getParent() != null) {
            return getParentId(temp.getParent(), 0) + "/" + temp.getDept_name();
        } else {
            if(tag == 1) {
                return temp.getDept_name();
            }
            return temp.getDept_name() + "/";
        }
    }
}
